
//
//  MyShopSearchView.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/12/12.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "MyShopSearchView.h"
#import "HomeSearchHeadView.h"
@interface MyShopSearchView()
@property(nonatomic,strong) MCButton *preBtn;
@property(nonatomic,strong) HomeSearchHeadView *searchView;
@property(nonatomic,strong) UIButton *searchBtn;
@end
@implementation MyShopSearchView
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = kWhite;
        [self setUpUI];
    }
    return self;
}
- (void)setIndex:(NSInteger)index
{
    _index = index;
    XLog(@"MyShopSearchViewself.index:%ld",self.index);
    if (self.index == 3) {
        CGFloat w=kUIScreen.size.width/4;
        CGFloat height = 40;
        for (int i = 0; i < 3; i++) {
            MCButton *btn = [[MCButton alloc]initWithFrame:CGRectMake(kUIScreen.size.width/3*i+(kUIScreen.size.width/3-w)/2, (kHeight-(i?height:height-5))/2, w, i?height:height-5)];
            [self addSubview:btn];
            btn.tag = i;
            [btn addTarget: self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
            NSArray *titles = @[@"搜索",@"服务列表",@"需求列表"];
            [btn setTitle:titles[i] forState:UIControlStateNormal];
            [btn setTitleColor:kOrangeBg forState:UIControlStateNormal];
            if (i != 0) {
                [btn setImage:[ImageColorHelper createImageWithColor:kOrangeBg]
                     forState:UIControlStateSelected];
                [btn setTitleColor:kLabel153Color forState:UIControlStateNormal];
                [btn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
            }
            if (i == 0) {
                btn.titleLabel.font = [UIFont systemFontOfSize:16];
                [btn setImage:[UIImage imageNamed:@"icon_15"] forState:UIControlStateNormal];
                btn.buttonStyle = imageLeft;
                btn.layer.masksToBounds = YES;
                btn.layer.cornerRadius = (height-5)/2;
                btn.layer.borderColor  = kOrangeBg.CGColor;
                btn.layer.borderWidth  = 2;
                btn.selected = YES;
            }else  if (i == 1) {
                btn.titleLabel.font = [UIFont systemFontOfSize:16];
                btn.buttonStyle = imageBottom;
                btn.selected = YES;
                self.preBtn = btn;
            }else {
                btn.titleLabel.font = [UIFont systemFontOfSize:14];
                btn.buttonStyle = imageBottom;
                btn.selected = NO;
            }
            [btn addTarget: self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        }
        
    }else {
        CGFloat w=kUIScreen.size.width/2;
        for (int i = 0; i < 2; i++) {
            MCButton *btn = [[MCButton alloc]initWithFrame:CGRectMake(w*i, 0, w, 30)];
            [self addSubview:btn];
            btn.tag = i+1;
            [btn addTarget: self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
            NSArray *titles = @[@"服务列表",@"需求列表"];
            [btn setTitle:titles[i] forState:UIControlStateNormal];
            [btn setTitleColor:kOrangeBg forState:UIControlStateSelected];
            [btn setTitleColor:kLabel153Color forState:UIControlStateNormal];
            [btn setImage:[ImageColorHelper createImageWithColor:kOrangeBg] forState:UIControlStateSelected];
            if (i == 0) {
                btn.titleLabel.font = [UIFont systemFontOfSize:16];
                btn.buttonStyle = imageBottom;
                btn.selected = YES;
                self.preBtn = btn;
            }else {
                [btn setTitleColor:kLabel153Color forState:UIControlStateNormal];
                btn.titleLabel.font = [UIFont systemFontOfSize:14];
                btn.buttonStyle = imageBottom;
                btn.selected = NO;
            }
            [btn addTarget: self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}
-(void)setUpUI
{
   
    
}
-(void)clickBtn:(MCButton *)clickBtn
{
    if (clickBtn.tag != 0) {
        clickBtn.selected = !clickBtn.selected;
        if (self.preBtn != clickBtn) {
            self.preBtn.selected = NO;
            self.preBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        }else {
        }
        clickBtn.selected = YES;
        clickBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        self.preBtn = clickBtn;
    }else {
        self.searchView = [[HomeSearchHeadView alloc]initWithFrame:CGRectMake(-kWidth, (kHeight-40)/2, kWidth*0.8, 40)];
//        self.searchView.delegate = self;
        //点击搜索
        __weak typeof (self)weakSelf = self;
        self.searchView.returnValueSearcBlock = ^(NSInteger selected, NSString * _Nonnull text) {
            XLog(@"text:%@",text);
            [weakSelf.searchView.tf resignFirstResponder];
            text.length?0:[WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"请输入您要搜索的内容"];
            if ([weakSelf.delegate respondsToSelector:@selector(myShopSearchView:withClickBtn:withSearchText:)]) {
                [weakSelf.delegate myShopSearchView:weakSelf withClickBtn:clickBtn withSearchText:text];
            }
        };
        [self.searchView.tf becomeFirstResponder];
        [UIView animateWithDuration:0.5 animations:^{
            self.searchView.transform = CGAffineTransformMakeTranslation(kWidth+11, 0);
            for (id obj in self.subviews) {
                if ([obj isKindOfClass:[MCButton class]]) {
                    XLog(@"obj:%@",obj);
                    MCButton *btn = (MCButton *)obj;
                    btn.alpha = 0;
                }
            }
        } completion:^(BOOL finished) {
            self.searchBtn = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.searchView.frame)+8, (kHeight-25)/2, kWidth*0.1, 25)];
            self.searchBtn.tag = 5;
            [self addSubview:self.searchBtn];
            [self.searchBtn setTitle:@"取消" forState:UIControlStateNormal];
            self.searchBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            self.searchBtn.layer.masksToBounds = YES;
            self.searchBtn.layer.cornerRadius = 3;
            self.searchBtn.layer.borderColor  = kOrangeBg.CGColor;
            self.searchBtn.layer.borderWidth  = 1;
            [self.searchBtn setTitleColor:kOrangeBg forState:UIControlStateNormal];
            [self.searchBtn addTarget: self action:@selector(clickSearchBtn:) forControlEvents:UIControlEventTouchUpInside];
            
        }];
        self.searchView.tf.backgroundColor = kBg;
        [self addSubview:self.searchView];
    }
    if ([self.delegate respondsToSelector:@selector(myShopSearchView:withClickBtn:)]) {
        [self.delegate myShopSearchView:self withClickBtn:clickBtn];
    }
}
-(void)clickSearchBtn:(UIButton *)clickSearchBtn
{
        [self.searchView removeFromSuperview];
        [self.searchBtn removeFromSuperview];
        for (id obj in self.subviews) {
            if ([obj isKindOfClass:[MCButton class]]) {
                XLog(@"obj:%@",obj);
                MCButton *btn = (MCButton *)obj;
                btn.alpha = 1;
            }
        }
    if ([self.delegate respondsToSelector:@selector(myShopSearchView:withClickBtn:withSearchText:)]) {
        [self.delegate myShopSearchView:self withClickBtn:clickSearchBtn withSearchText:@""];
    }

}

@end
